class Solution {
    public int maximalRectangle(char[][] matrix) {
        if (matrix.length == 0 || matrix[0].length == 0) {
            return 0;
        }

        int maxarea = 0;
        int[][] temp = new int[matrix.length][matrix[0].length];

        for (int i = 0; i < matrix.length; i++) {
            for (int j = 0; j < matrix[0].length; j++) {
                if (i == 0) {
                    temp[i][j] = matrix[i][j] - '0';
                } else {
                    temp[i][j] = (matrix[i][j] == '0') ? 0 : (temp[i - 1][j] + 1);
                }

                int min = temp[i][j];
                for (int k = j; k >= 0; k--) {
                    if (min == 0) {
                        break;
                    }
                    if (min > temp[i][k]) {
                        min = temp[i][k];
                    }
                    maxarea = Math.max(maxarea, min * (j - k + 1));
                }
            }
        }
        return maxarea;
    }

}